import cgi
import wsgiref.handlers
import os

from google.appengine.ext.webapp import template
from google.appengine.api import users
from google.appengine.ext import webapp

class MainPage(webapp.RequestHandler):
  def get(self):
    if users.get_current_user():
      header_text = 'Logged in as ' + users.get_current_user().nickname()
      url = users.create_logout_url(self.request.uri)
      url_linktext = 'Logout'
      script_tags = '<script type="text/javascript" language="javascript" src="gui/com.googlecode.olabs.gui.Olabs.nocache.js"></script><iframe src="javascript:\'\'" id="__gwt_historyFrame" style="position:absolute;width:0;height:0;border:0"></iframe>'
    else:
      header_text = 'Not currently logged in';
      url = users.create_login_url(self.request.uri)
      url_linktext = 'Login'
      script_tags = ' '

    template_values = {
      'title_text': 'CAMS',
      'header_text': header_text,
      'header_url': url,
      'header_url_linktext': url_linktext,
      'script_tags': script_tags
      }

    path = os.path.join(os.path.dirname(__file__), 'index.html')
    self.response.out.write(template.render(path, template_values))

def main():
  application = webapp.WSGIApplication(
      [
        ('/', MainPage)
      ],
      debug=True)
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
  main()
